package com.sita.bluetactivity.MineFragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sita.bluetactivity.Base.BaseApplication;
import com.sita.bluetactivity.Base.SpUtils;
import com.sita.bluetactivity.LoginAndRegister.LoginActivity;
import com.sita.bluetactivity.R;
import com.squareup.picasso.Picasso;


import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Jason on 2016/9/21.
 */
public class MineSettingFragment extends Fragment implements View.OnClickListener{
    private RelativeLayout myVehicle,about;
    private TextView logoutTx;
    private CircleImageView userIcon;
    private TextView nickname;
    private String url;
    private String nickName;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();

    }
    private void initView(){
        myVehicle = (RelativeLayout) getActivity().findViewById(R.id.my_vehicle);
        about = (RelativeLayout) getActivity().findViewById(R.id.app_about);
        logoutTx = (TextView) getActivity().findViewById(R.id.logout);
        userIcon = (CircleImageView) getActivity().findViewById(R.id.user_icon);
        nickname = (TextView) getActivity().findViewById(R.id.user_nickname);
    }
    private void initData(){
        myVehicle.setOnClickListener(this);
        about.setOnClickListener(this);
        logoutTx.setOnClickListener(this);
        userIcon.setOnClickListener(this);

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden){

        }else {

        }
    }

    @Override
    public void onStart() {
        super.onStart();
        url = SpUtils.getString("UserIcon",null,BaseApplication.getContext());
        nickName = SpUtils.getString("UserNickName",null,BaseApplication.getContext());
        Picasso.with(BaseApplication.getContext()).load(url).placeholder(R.mipmap.no_icon).into(userIcon);
        if (nickName==null){
            nickname.setText("昵称");
        }
        else {
            nickname.setText(nickName);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.my_vehicle:
                BindVehicleActivity.jumpIntent(getActivity());
                break;
            case R.id.app_about:
                AboutActivity.JumpIntent(getActivity());
                break;
            case R.id.logout:
                final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setMessage("确定退出？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SpUtils.putBoolean("isFirst",true, BaseApplication.getContext());
                        LoginActivity.jumpLoginIntent(getActivity());
                        getActivity().finish();
                        dialog.dismiss();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                      dialog.dismiss();
                    }
                });
                builder.setCancelable(true);
                builder.show();

                break;
            case R.id.user_icon:
                UserInfoActivity.JumpIntent(getActivity());
                break;
        }
    }
}
